import java.util.HashMap;
import java.util.Map;

public class Leetcode {
}

//leetcode：560：和为k的子数组
class Solution1 {
    public int subarraySum(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<Integer,Integer>();
        hash.put(0,1);
        int sum = 0,ret = 0;
        for(int x : nums){
            sum += x;
            ret += hash.getOrDefault(sum-k,0);
            hash.put(sum,hash.getOrDefault(sum,0)+1);
        }
        return ret;
    }
}

//leetcode:974:和可被k整除的子数组
class Solution2 {
    public int subarraysDivByK(int[] nums, int k) {
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0 % k,1);
        int sum = 0,ret = 0;
        for(int x : nums){
            sum += x;
            int r = (sum%k+k)%k;
            ret += hash.getOrDefault(r,0);
            hash.put(r,hash.getOrDefault(r,0)+1);
        }
        return ret;
    }
}

////leetcode:2588：统计美丽子数组数目
class Solution3 {
    public long beautifulSubarrays(int[] nums) {
        Map<Integer,Integer> map = new HashMap<>();
        int m = 0;
        int ret = 0;
        map.put(0,1);
        for(int x : nums){
            m ^= x;
            ret += map.getOrDefault(m,0);
            map.put(m,map.getOrDefault(m,0)+1);
        }
        return ret;
    }
}